package jdbc;

import java.sql.*;

public class Util {
    public static void register(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("注册驱动成功");
        } catch (ClassNotFoundException e) {
            System.out.println("注册驱动失败");
        }
    }

    public static Connection getConnection(){
        register();
        String url="jdbc:mysql://localhost:3306/lly";
        String user="root";
        String pass="123";
        Connection con= null;
        try {
            con = DriverManager.getConnection(url,user,pass);
        } catch (SQLException e) {
            System.out.println("连接数据库出错");
        }
        return con;

    }

    public static void close(Connection con, Statement st, ResultSet rs){
        try {
            if (con != null) {
                con.close();
            }
            if (st != null) {
                st.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
